package com.jy.rhin.parse;


import com.jy.rhin.infra.cda.XPath;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Element;
import org.seimicrawler.xpath.JXNode;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
/**
 * @author WeiQ
 * @date 2023/3/30 15:37
 */
@Slf4j
@Component
public class CodeValueSelector implements Selector {

    @Resource
    DirectValueSelector directValueParser;

    @Override
    public String selectValue(JXNode root, XPath xPath) {
        String codePath = xPath.codePath();
        JXNode jxNode = root.selOne(codePath);
        if (Objects.nonNull(jxNode)) {
            Element element = jxNode.asElement().parent();
            return directValueParser.selectValue(new JXNode(element), xPath);
        } else {
            //log.info("当前文档不存在该节点{}", codePath);
            return null;
        }
    }
}
